package com.bawei.lib_common.base.state

import android.content.Context
import android.os.Handler
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AlphaAnimation
import android.widget.Button
import android.widget.FrameLayout
import com.bawei.lib_common.R


/**
 *作者：lxy
 *功能:
 *时间：2025/5/29
 */
abstract class UILoader(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    FrameLayout(context, attrs, defStyleAttr) {

    private var mLoadingView: View? = null
    private var mErrorView: View? = null
    private var mEmptyView: View? = null
    private var mSuccessView: View? = null
    private var mUnLoginView: View? = null
    private var mNoFollowView: View? = null

    var onLoginClick: (() -> Unit)? = null
    var mCurrentState = UIState.NONE
    protected var mIRetryClickListener: IRetryClickListener? = null

    private var isRefresh = false

    constructor(context: Context) : this(context, null) {}


    /**
     * 更新UI状态方法
     */
    fun updateState(state: UIState) {
        if (mCurrentState != state) {
            mCurrentState = state

            Handler().post { switchUIByState() }
        }
    }

    /**
     * 根据当前状态切换UI
     */
    private fun switchUIByState() {
        /**成功 */
        if (mSuccessView == null) {
            mSuccessView = loadSuccessView(this)
            addView(mSuccessView)
        }


        //设置是否显示
        if (mCurrentState == UIState.SUCCESS && mSuccessView != null) {
            mSuccessView!!.startAnimation(alphaAniShow)

        }
        mSuccessView!!.visibility = if (mCurrentState == UIState.SUCCESS) VISIBLE else GONE


        //刷新时不切换/成功时切换
        if (!isRefresh || mCurrentState == UIState.SUCCESS) {

            mLoadingView!!.visibility = if (mCurrentState == UIState.LOADING) VISIBLE else GONE
            mErrorView!!.visibility = if (mCurrentState == UIState.NETWORK_ERROR) VISIBLE else GONE
            mEmptyView!!.visibility = if (mCurrentState == UIState.EMPTY) VISIBLE else GONE
            mUnLoginView!!.visibility = if (mCurrentState == UIState.UN_LOGIN) VISIBLE else GONE
            mNoFollowView!!.visibility = if (mCurrentState == UIState.NO_FOLLOW) VISIBLE else GONE
        }



        isRefresh = false
    }


    /**
     * 加载成功布局文件，该方法子类必选实现
     */
    protected abstract fun loadSuccessView(container: ViewGroup?): View?

    /**
     * 加载加载中 布局文件
     */

    /**
     * 加载空布局文件
     */

    /**
     * 加载错误布局文件
     */


    /**
     * 重试点击方法
     *
     * @param listener 监听接口
     */
    fun setOnRetryClickListener(listener: IRetryClickListener?) {
        mIRetryClickListener = listener
    }

    private var alphaAniShow: AlphaAnimation? = null
    private fun alphaAnimation() {
        alphaAniShow = AlphaAnimation(0f, 1f)
        alphaAniShow!!.duration = 500
    }

    init {
        switchUIByState() //切换UI状态-当前为初始状态NONE
        alphaAnimation()

    }
}